package com.bartender.print.controller;

import cn.hutool.core.date.DateUtil;
import com.bartender.print.model.BoxInfo;
import com.bartender.print.model.SmsWmsBarcodeInfo;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("")
public class BartenderPrint {

    @PostMapping(value = "/ReceivePrint")
    protected String ReceivePrint(@RequestBody List<SmsWmsBarcodeInfo> smsWmsBarcodeInfos)  {
        //要运行COM组件，需要先将jacob-1.19-x64.dll放到jdk的bin目录下，不然会报错。如果是32位jdk放入32位的dll文件
        ComThread.InitSTA();
        //加载Bartender进程，需要先安装Bartender，我安装的是BT2019R4版本。
        ActiveXComponent btApp = new ActiveXComponent("BarTender.Application");

        //加载Bartender模板控件
        Dispatch btFormats = btApp.getProperty("Formats").toDispatch();
        //调用模板
        Dispatch btFormat = Dispatch.call(btFormats, "Open", "C:\\JavaBarTenderPrint\\model\\BARCODE.btw", false, "").toDispatch();
        for (SmsWmsBarcodeInfo smsWmsBarcodeInfo : smsWmsBarcodeInfos) {
            log.info("{} 打印条码：{}",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"),smsWmsBarcodeInfo.getTblBarcode());
            //传递参数，参数名称要和Bartender中设置的名称一样，不然会失败
            Dispatch.call(btFormat, "SetNamedSubStringValue", "SN", smsWmsBarcodeInfo.getTblBarcode());
            Dispatch.call(btFormat, "SetNamedSubStringValue", "MPN", smsWmsBarcodeInfo.getMpn());
            Dispatch.call(btFormat, "SetNamedSubStringValue", "数量", smsWmsBarcodeInfo.getTblNum());
            Dispatch.call(btFormat, "SetNamedSubStringValue", "湿敏等级", smsWmsBarcodeInfo.getMcrMsdCode());
            Dispatch.call(btFormat, "SetNamedSubStringValue", "MAKE_DATE", DateUtil.format(smsWmsBarcodeInfo.getTblCreatedate(),"yy/MM/dd"));
            Dispatch.call(btFormat, "SetNamedSubStringValue", "制造商批号", smsWmsBarcodeInfo.getTblManufacturerBat());
            Dispatch.call(btFormat, "SetNamedSubStringValue", "采购单号", smsWmsBarcodeInfo.getTblPoNum());
            Dispatch.call(btFormat, "SetNamedSubStringValue", "ITEM",smsWmsBarcodeInfo.getTblItemcode());
            Dispatch.call(btFormat, "SetNamedSubStringValue", "项目号", smsWmsBarcodeInfo.getProjectCode());
            Dispatch.call(btFormat, "SetNamedSubStringValue", "制造商", smsWmsBarcodeInfo.getTblManufacturerCode());
            Dispatch.call(btFormat, "SetNamedSubStringValue", "EMP", smsWmsBarcodeInfo.getUserAccount());

            //设置打印份数，默认是1份可以不设置
            Dispatch printSetup = Dispatch.get(btFormat, "PrintSetup").toDispatch();
            Dispatch.put(printSetup, "IdenticalCopiesOfLabel", 1);

            //打印，Bartender会自动选择模板中设置的打印机打印
            Dispatch.call(btFormat, "PrintOut", false, false);
        }

        //打印完成，关闭模板控件
        Dispatch.call(btFormat, "Close", 0);
        //关闭Bartender进程
        Dispatch.call(btApp, "Quit", 0);
        ComThread.Release();
        return "打印成功";
    }
    @PostMapping(value = "/ReturnPrint")
    protected String ReturnPrint(@RequestBody List<SmsWmsBarcodeInfo> smsWmsBarcodeInfos)  {
        //要运行COM组件，需要先将jacob-1.19-x64.dll放到jdk的bin目录下，不然会报错。如果是32位jdk放入32位的dll文件
        ComThread.InitSTA();
        //加载Bartender进程，需要先安装Bartender，我安装的是BT2019R4版本。
        ActiveXComponent btApp = new ActiveXComponent("BarTender.Application");

        //加载Bartender模板控件
        Dispatch btFormats = btApp.getProperty("Formats").toDispatch();
        //调用模板
        Dispatch btFormat = Dispatch.call(btFormats, "Open", "C:\\JavaBarTenderPrint\\model\\RETURN_BARCODE.btw", false, "").toDispatch();
        for (SmsWmsBarcodeInfo smsWmsBarcodeInfo : smsWmsBarcodeInfos) {
            log.info("{} 打印条码：{}",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"),smsWmsBarcodeInfo.getTblBarcode());
            //传递参数，参数名称要和Bartender中设置的名称一样，不然会失败
            Dispatch.call(btFormat, "SetNamedSubStringValue", "数量", smsWmsBarcodeInfo.getTblNum());
            Dispatch.call(btFormat, "SetNamedSubStringValue", "NOW_DATE", DateUtil.format(new Date(),"yyyy.MM.dd"));
            Dispatch.call(btFormat, "SetNamedSubStringValue", "EMP", smsWmsBarcodeInfo.getUserAccount());

            //设置打印份数，默认是1份可以不设置
            Dispatch printSetup = Dispatch.get(btFormat, "PrintSetup").toDispatch();
            Dispatch.put(printSetup, "IdenticalCopiesOfLabel", 1);

            //打印，Bartender会自动选择模板中设置的打印机打印
            Dispatch.call(btFormat, "PrintOut", false, false);
        }

        //打印完成，关闭模板控件
        Dispatch.call(btFormat, "Close", 0);
        //关闭Bartender进程
        Dispatch.call(btApp, "Quit", 0);
        ComThread.Release();
        return "打印成功";
    }
    @PostMapping(value = "/BoxPrint")
    protected String BoxPrint(@RequestBody BoxInfo boxInfo)  {
        //要运行COM组件，需要先将jacob-1.19-x64.dll放到jdk的bin目录下和jre的bin目录下，不然会报错。如果是32位jdk放入32位的dll文件
        ComThread.InitSTA();
        //加载Bartender进程，需要先安装Bartender，我安装的是BT2019R4版本。
        ActiveXComponent btApp = new ActiveXComponent("BarTender.Application");
        String boxNo = boxInfo.getBoxNo();
        System.out.println(boxNo);
        //加载Bartender模板控件
        Dispatch btFormats = btApp.getProperty("Formats").toDispatch();
        //调用模板
        Dispatch btFormat = Dispatch.call(btFormats, "Open", "C:\\JavaBarTenderPrint\\model\\NEW_BOX.btw", false, "").toDispatch();
        Dispatch.call(btFormat, "SetNamedSubStringValue", "boxNo", boxNo);
        Dispatch.call(btFormat, "SetNamedSubStringValue", "modelCode", boxInfo.getModelCode());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "boxQty", boxInfo.getBoxQty());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "modelVer", boxInfo.getModelVer());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "batch", boxInfo.getBatch());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "mpn", boxInfo.getMpn());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "createTime", DateUtil.format(boxInfo.getCreateTime(),"yyyy/MM/dd"));
        String allInfo="{\"BOXID\":\""+boxNo+"\",\"PN\":\""+boxInfo.getModelCode()+"\",\"QTY\":\""+boxInfo.getBoxQty()+"\",\"Version\":\""
                + boxInfo.getModelVer() +"\",\"Verdor\":\"GT\",\"LOT\":\""+ Objects.toString(boxInfo.getBatch(),"")
                + "\",\"MPN\":\""+Objects.toString(boxInfo.getMpn(),"")+"\",\"Packing Date\":\""+
                DateUtil.format(boxInfo.getCreateTime(),"yyyy/MM/dd"+"\"}");
        System.out.println(allInfo);
        Dispatch.call(btFormat, "SetNamedSubStringValue", "allInfo",allInfo );
        Dispatch.call(btFormat, "SetNamedSubStringValue", "XH", Integer.valueOf(boxNo.substring(boxNo.length()-5)));
        Dispatch.call(btFormat, "SetNamedSubStringValue", "modelName", "描述："+boxInfo.getModelName());

        //设置打印份数，默认是1份可以不设置
        Dispatch printSetup = Dispatch.get(btFormat, "PrintSetup").toDispatch();
        Dispatch.put(printSetup, "IdenticalCopiesOfLabel", 1);

        //打印，Bartender会自动选择模板中设置的打印机打印
        Dispatch.call(btFormat, "PrintOut", false, false);

        //打印完成，关闭模板控件
        Dispatch.call(btFormat, "Close", 0);


        List<String> boxBarcodeInfos = boxInfo.getBoxBarcodeInfos();
        int pageSize=40;
        int total = boxBarcodeInfos.size();
        int totalPage =  total % pageSize==0?total/pageSize:total/pageSize+1;
        for (int page=1;page<=totalPage;page++){
            //加载Bartender模板控件
            Dispatch btFormats1= btApp.getProperty("Formats").toDispatch();
            //调用模板
            Dispatch btFormat1 = Dispatch.call(btFormats1, "Open", "C:\\JavaBarTenderPrint\\model\\NEW_QN_LIST.btw", false, "").toDispatch();
            int count=page!=totalPage?pageSize:total%pageSize;
            Dispatch.call(btFormat1, "SetNamedSubStringValue", "modelCode", boxInfo.getModelCode());
            Dispatch.call(btFormat1, "SetNamedSubStringValue", "count", count);
            Dispatch.call(btFormat1, "SetNamedSubStringValue", "boxNo", boxNo);
            Dispatch.call(btFormat1, "SetNamedSubStringValue", "createTime", DateUtil.format(boxInfo.getCreateTime(),"yyyy/MM/dd"));
            Dispatch.call(btFormat1, "SetNamedSubStringValue", "pageInfo", page+" of "+totalPage);

            for (int i=0;i<pageSize;i++){
                if (i<count){
                    String barcode = boxBarcodeInfos.get(i+pageSize*(page-1));
                    log.info("{} 打印箱码：{}",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"),barcode);
                    Dispatch.call(btFormat1, "SetNamedSubStringValue", "CODE"+(i+1), barcode);
                }else {
                    Dispatch.call(btFormat1, "SetNamedSubStringValue", "CODE"+(i+1), "");
                }
            }
            //设置打印份数，默认是1份可以不设置
            Dispatch printSetup1 = Dispatch.get(btFormat1, "PrintSetup").toDispatch();
            Dispatch.put(printSetup1, "IdenticalCopiesOfLabel", 1);

            //打印，Bartender会自动选择模板中设置的打印机打印
            Dispatch.call(btFormat1, "PrintOut", false, false);

            //打印完成，关闭模板控件
            Dispatch.call(btFormat1, "Close", 0);
        }

        //关闭Bartender进程
        Dispatch.call(btApp, "Quit", 0);
        ComThread.Release();
        return "打印成功";
    }
    @PostMapping(value = "/BoxOldPrint")
    protected String BoxOldPrint(@RequestBody BoxInfo boxInfo)  {
        //要运行COM组件，需要先将jacob-1.19-x64.dll放到jdk的bin目录下和jre的bin目录下，不然会报错。如果是32位jdk放入32位的dll文件
        ComThread.InitSTA();
        //加载Bartender进程，需要先安装Bartender，我安装的是BT2019R4版本。
        ActiveXComponent btApp = new ActiveXComponent("BarTender.Application");
        String boxNo = boxInfo.getBoxNo();
        System.out.println(boxNo);
        //加载Bartender模板控件
        Dispatch btFormats = btApp.getProperty("Formats").toDispatch();
        //调用模板
        Dispatch btFormat = Dispatch.call(btFormats, "Open", "C:\\JavaBarTenderPrint\\model\\OLD_BOX.btw", false, "").toDispatch();
        Dispatch.call(btFormat, "SetNamedSubStringValue", "boxNo", boxNo);
        Dispatch.call(btFormat, "SetNamedSubStringValue", "modelCode", boxInfo.getModelCode());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "boxQty", boxInfo.getBoxQty());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "modelVer", boxInfo.getModelVer());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "batch", boxInfo.getBatch());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "moNo", boxInfo.getMoNo());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "extra", boxInfo.getExtra());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "dataCode", boxInfo.getDataCode());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "createName", boxInfo.getCreateName());
        //Dispatch.call(btFormat, "SetNamedSubStringValue", "mpn", boxInfo.getMpn());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "createTime", DateUtil.format(boxInfo.getCreateTime(),"yyyy/MM/dd"));
        String allInfo="{\"BOXID\":\""+boxNo+"\",\"PN\":\""+boxInfo.getModelCode()+"\",\"QTY\":\""+boxInfo.getBoxQty()+"\",\"Version\":\""
                + boxInfo.getModelVer() +"\",\"Verdor\":\"GT\",\"LOT\":\""+ Objects.toString(boxInfo.getBatch(),"")
                + "\",\"MPN\":\""+Objects.toString(boxInfo.getMpn(),"")+"\",\"Packing Date\":\""+
                DateUtil.format(boxInfo.getCreateTime(),"yyyy/MM/dd"+"\"}");
        System.out.println(allInfo);
        //Dispatch.call(btFormat, "SetNamedSubStringValue", "allInfo",allInfo );
        Dispatch.call(btFormat, "SetNamedSubStringValue", "XH", Integer.valueOf(boxNo.substring(boxNo.length()-5)));
        Dispatch.call(btFormat, "SetNamedSubStringValue", "modelName", boxInfo.getModelName());
        Dispatch.call(btFormat, "SetNamedSubStringValue", "SN",boxInfo.getBoxBarcodeInfos()==null?"":boxInfo.getBoxBarcodeInfos().get(0));

        //设置打印份数，默认是1份可以不设置
        Dispatch printSetup = Dispatch.get(btFormat, "PrintSetup").toDispatch();
        Dispatch.put(printSetup, "IdenticalCopiesOfLabel", 1);

        //打印，Bartender会自动选择模板中设置的打印机打印
        Dispatch.call(btFormat, "PrintOut", false, false);

        //打印完成，关闭模板控件
        Dispatch.call(btFormat, "Close", 0);


        List<String> boxBarcodeInfos = boxInfo.getBoxBarcodeInfos();
        int pageSize=40;
        int total = boxBarcodeInfos.size();
        int totalPage =  total % pageSize==0?total/pageSize:total/pageSize+1;
        for (int page=1;page<=totalPage;page++){
            //加载Bartender模板控件
            Dispatch btFormats1= btApp.getProperty("Formats").toDispatch();
            //调用模板
            Dispatch btFormat1 = Dispatch.call(btFormats1, "Open", "C:\\JavaBarTenderPrint\\model\\OLD_QN_LIST.btw", false, "").toDispatch();
            int count=page!=totalPage?pageSize:total%pageSize;
            Dispatch.call(btFormat1, "SetNamedSubStringValue", "modelCode", boxInfo.getModelCode());
            Dispatch.call(btFormat1, "SetNamedSubStringValue", "count", count);
            Dispatch.call(btFormat1, "SetNamedSubStringValue", "boxNo", boxNo);
            Dispatch.call(btFormat1, "SetNamedSubStringValue", "createTime", DateUtil.format(boxInfo.getCreateTime(),"yyyy/MM/dd"));
            Dispatch.call(btFormat1, "SetNamedSubStringValue", "pageInfo", page+" of "+totalPage);

            for (int i=0;i<pageSize;i++){
                if (i<count){
                    String barcode = boxBarcodeInfos.get(i+pageSize*(page-1));
                    log.info("{} 打印箱码：{}",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"),barcode);
                    Dispatch.call(btFormat1, "SetNamedSubStringValue", "CODE"+(i+1), barcode);
                }else {
                    Dispatch.call(btFormat1, "SetNamedSubStringValue", "CODE"+(i+1), "");
                }
            }
            //设置打印份数，默认是1份可以不设置
            Dispatch printSetup1 = Dispatch.get(btFormat1, "PrintSetup").toDispatch();
            Dispatch.put(printSetup1, "IdenticalCopiesOfLabel", 1);

            //打印，Bartender会自动选择模板中设置的打印机打印
            Dispatch.call(btFormat1, "PrintOut", false, false);

            //打印完成，关闭模板控件
            Dispatch.call(btFormat1, "Close", 0);
        }

        //关闭Bartender进程
        Dispatch.call(btApp, "Quit", 0);
        ComThread.Release();
        return "打印成功";
    }
}
